let form = layui.form

// 获取轮播图数据渲染到模板引擎上
function dataSwiperList() {
  $.ajax({
    type: "get",
    url: "admin/swipers",
    success: function (res) {
      let htmlStr = template("tpl_list", res);
      $(".layui-table tbody").html(htmlStr)
    }
  })
}
dataSwiperList();

// 轮播图切换效果 
$(".layui-table tbody").on("click", ".layui-badge", function (e) {
  let id = $(e.target).data("id");
  let a = $(e.target).data("status");
  $.ajax({
    type: "PUT",
    url: "admin/swipers/" + id,
    data: {
      status: a
    },
    success: function (res) {
      if (res.status === 0) {
        layer.msg(res.message)
        dataSwiperList();
      }
    }
  })
})

// 删除
$(".layui-table tbody").on("click", ".delete", function (e) {
  let id = $(e.target).data("id");
  layer.confirm('确定要删除嘛?', function (index) {
    $.ajax({
      type: "delete",
      url: "admin/swipers/" + id,
      success: function (res) {
        console.log(res);
        if (res.status === 0) {
          layer.close(index);
          dataSwiperList();
        }
      }
    })
  });
})

// 批量上传图片
$("#test1").on("click", function () {
  $("#file").trigger("click")
})

$("#file").on("change", function (e) {
  e.preventDefault();
  let files = e.target.files
  var fd = new FormData()
  $.each(files, function (index, item) {
    fd.append('swipers', item)
  })
  $.ajax({
    type: "POST",
    url: "admin/swipers",
    data: fd,
    processData: false,
    contentType: false,
    success: function (res) {
      if (res.status === 0) {
        dataSwiperList();
      }
    }
  })
})